#!/usr/bin/env python
# coding=utf-8

import os
import sys
import codecs
import string
import logging
import glob

# set log
logFileName = 'CheckEncoding.log'
def init_log():
    logger = logging.getLogger()
    hdlr = logging.FileHandler(logFileName)
    formatter = logging.Formatter('%(asctime)s %(levelname)s:: %(message)s')
    hdlr.setFormatter(formatter)
    logger.addHandler(hdlr)
    logger.setLevel(logging.INFO)
    return logger

log = init_log()

# output file
outfile = None

def check_file(filename):
	'Check the file and output to outfile.\n'
	try:
		codecs.open(filename, 'r', 'gb2312').read()
		return	True
	except:
		fpath, fname = os.path.split(filename)
		print>>outfile, 'Filename : ' + fname
	f = codecs.open(filename, 'r', 'gbk')
	lines = f.readlines()
	for lineno in range(len(lines)):
		line = lines[lineno]
		while line[-1].isspace():
			line = line[:-1]
		try:
			line.encode('gb2312')
		except:
			print>>outfile, '\tLine %d :\t%s'%(lineno, line)
			for c in line:
				try:
					c.encode('gb2312')
				except:
					print>>outfile, u'\t\t' + c
	print>>outfile
	return False


def main():
	if len(sys.argv) != 3:
		print('Usage:')
		print('CheckEncoding.py [File|Folder] output_file')
		sys.exit(0)
	global outfile
	outfile = codecs.open(sys.argv[2], 'w', 'utf-8')
	if os.path.isfile(sys.argv[1]):
		check_file(sys.argv[1])
	elif os.path.isdir(sys.argv[1]):
		for fn in glob.glob(os.path.join(sys.argv[1], '*.txt')):
			check_file(fn)
	else:
		print>>sys.stderr, 'The first argument is not a filepath or folder'
		sys.exit(1)

if __name__ == '__main__':
	main()

